package com.dingup.homework.controller.index;

import com.dingup.api.Util.RedisUtils;
import com.dingup.api.base.HttpResContent;
import com.dingup.homework.service.HwHomeworkService;
import com.dingup.homework.service.TeacherCorrectService;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;


/**
 * Created by wancheng on 2018/11/21.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private TeacherCorrectService teacherCorrectService;
    @Autowired
    private HwHomeworkService hwHomeworkService;

    @RequestMapping(value = "/get_related_count.do", method = RequestMethod.POST)
    public void listByPaperId(HttpServletRequest request,
                              HttpServletResponse response) {
        HttpResContent<Map<String, Object>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取首页相关统计");
        try {
            String userId = RedisUtils.getInstance().getUserId(request);
            Integer unCorrectHomeworkCount = teacherCorrectService.getUnCorrectListCount(userId);
            Integer expiredHomeworkCount = hwHomeworkService.getExpireCountFromYesterday(userId);
            Map<String, Object> res = new HashMap<>();
            res.put("unCorrectHomeworkCount", unCorrectHomeworkCount);
            res.put("expiredHomeworkCount", expiredHomeworkCount);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }
}
